package cms.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 功能描述：提供jdbc连接MySQL的Connection对象
 *
 * @Author: 余辉
 * @Date: 2021/4/5 9:59
 */
public class DButils {
    private static String db_Driver;
    private static String url;
    private static String db_name;
    private static String db_password;
    private static Properties prop;


    /**
    * 功能描述: 创建jdbc连接
    * 适用场景: mysql
    * 适用前提:
    * @Author: yuhui
    * @Date: 2021/4/5 10:20
    * @param
    * @return
    */
    public static Connection createConnection(){
        Connection con = null;
        try {
            con = DriverManager.getConnection(url,db_name,db_password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    /*properties配置文件读取jdbc连接参数*/
    static{
        prop = new Properties();
        InputStream cfgInput = DButils.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            prop.load(cfgInput);
            db_Driver = prop.getProperty("jdbc_driver");
            url = prop.getProperty("jdbc_url");
            db_name = prop.getProperty("jdbc_userName");
            db_password = prop.getProperty("jdbc_userPass");

            Class.forName(db_Driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
